// pages/patientPages/my/my.js
const app = getApp() //获取app实列
const ajax = require("../../../../utils/ajax.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {}
  },
  sexClick: function (event) {
    let a = this.data.userInfo
    a.sex = event.currentTarget.dataset.sex
    this.setData({
      userInfo: a
    })
  },
  changeInput:function(e){
    let key=e.currentTarget.dataset.key;
    let obj=this.data.userInfo;
    obj[key]=e.detail.value;
    this.setData({
      userInfo:obj
    })
  },
  submitSet:function(){
    ajax.request({
      url: app.url.memberUpdateInfo,
      data:this.data.userInfo
    }).then(res=>{
      if(res.code!==200){
        return wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }
      let info=JSON.stringify(this.data.userInfo)
      wx.setStorageSync("userInfo", info)
      wx.showToast({
        title: '保存成功',
        icon:"none",
        success:function(){
          wx.navigateBack({
           delta:1 
          })
        }
      })
    })
  },
  getUserInfo:function(){
    let that=this
    ajax.request({
      url: app.url.userInfo
    }).then(res=>{
      if(res.code==200){
        let str = JSON.stringify(res.data)
        wx.setStorageSync("userInfo", str)
        that.setData({
          userInfo:res.data
        })
      }else{
        return wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }
    })
  },
  uploadImg: function() {
    let that = this;
    
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        let info = JSON.parse(wx.getStorageSync("userInfo"))
        let token = wx.getStorageSync("token")
        wx.showLoading({
          title: '拼命上传中..',
        })
        wx.uploadFile({
          url: app.url.upload, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            token: token
          },
          success(data) {
            info.avatar = JSON.parse(data.data).data.path
            console.log(info.avatar)
            ajax.request({
              url: app.url.userUpdateAvatar,
              data: info
            }).then(res => {
              if (res.code == 200) {
                that.getUserInfo()
                wx.hideLoading()
              }
            })
          }
        })
      }
    })
  },
  backLogin: function() {
    //退出登录
    wx.removeStorageSync("token")
    wx.removeStorageSync("userInfo")
    wx.removeStorageSync("openid")
    wx.reLaunch({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let user = wx.getStorageSync("userInfo");
    if (user) {
      let userInfo = JSON.parse(user)
      this.setData({
        userInfo: userInfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})